package com.ucomm.talent.activity;
import java.util.Random;
import com.ucomm.talent.AppConstants;
import com.ucomm.talent.AppException;
import com.ucomm.talent.R;
import com.ucomm.talent.manager.SearchManager;
import com.ucomm.talent.widget.CustomProgressDialog;
import com.ucomm.talent.widget.KeywordsFlow;
import com.ucomm.talent.widget.SearchBarView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class SearchActivityNew extends Activity {
	
	private String[] keywords = null;
	private KeywordsFlow keywordsFlow;
	private  SearchManager searchManager;
	private CustomProgressDialog customProgressDialog;
	private Spinner spinner;
	private Button btSearch;
	private EditText etSearch;
	private SearchBarView searchBarView;
	   Handler mHandler = new Handler(){
	        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            switch (what){
                case AppConstants.MsgStatus.MSG_ERROR:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                    ((AppException)msg.obj).makeToast(SearchActivityNew.this);
                    break;
                case AppConstants.MsgStatus.MSG_SUCCUSS:
                    if (customProgressDialog != null && customProgressDialog.isShowing())
                        customProgressDialog.dismiss();
                     keywords = (String[]) msg.obj;
	                  // 添加
		     		feedKeywordsFlow(keywordsFlow, keywords);
		     		keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
		     		keywordsFlow.setOnTouchListener(new KeywordsFlowTouchListener());
                    break;
                case AppConstants.MsgStatus.MSG_LOADING:
                    if (customProgressDialog != null && !customProgressDialog.isShowing())
                        customProgressDialog.show();
                    break;
                default:
                    break;
            }
	            super.handleMessage(msg);
        }
    };
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.searchactivitynew_layout);
		searchBarView = (SearchBarView) findViewById(R.id.search_bar_view);
		etSearch = (EditText) searchBarView.findViewById(R.id.et_search);
		//etSearch = (EditText) findViewById(R.id.et_search);
		searchManager = new SearchManager(this,mHandler);
	    searchManager.searchDataFromNetwrok(AppConstants.HOT_SEARCH);
		keywordsFlow = (KeywordsFlow) findViewById(R.id.frameLayout1);
		keywordsFlow.setDuration(800l);
		keywordsFlow.setOnItemClickListener(new KeywordsFlowItemClickListener());
		btSearch = (Button) findViewById(R.id.bt_search);
		
		
	}
	//添加keywords
	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
		Random random = new Random();
		for (int i = 0; i < KeywordsFlow.MAX; i++) {
			int ran = random.nextInt(arr.length);
			String tmp = arr[ran];
			keywordsFlow.feedKeyword(tmp);
		}
	}
	class KeywordsFlowItemClickListener implements OnClickListener{
		@Override
		public void onClick(View v) {

				if (v instanceof TextView) {
				String keyword = ((TextView) v).getText().toString();
				if(etSearch != null){
					etSearch.setText(keyword);
				}
				}
			}
		}
	class KeywordsFlowTouchListener implements OnTouchListener{
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(v !=null){
				keywordsFlow.rubKeywords();
				// keywordsFlow.rubAllViews();
				feedKeywordsFlow(keywordsFlow, keywords);
				keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
			}
			return false;
		}
	}
	

}
